/**
 * @Title: stationController.java
 * @Package tt.dz.mvc.controller.sys
 * @author pengyu
 * @date 2015年12月17日 上午10:21:55
 * @version V1.0
 */
package tt.dz.mvc.controller.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.ast.Literal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tt.dz.dao.ext.charger.OmsChargerDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsStoreFile;
import tt.dz.entity.charger.OmsCharger;
import tt.dz.entity.charger.OmsChargerErrorRecord;
import tt.dz.service.charger.OmsChargerErrorRecordService;
import tt.dz.util.ActionUtils;
import tt.dz.util.OmsConstant;
import tt.dz.util.StringUtil;

@Controller
@RequestMapping("chargerErrorRecordApi")
public class ChargerErrorRecordController {

	@Resource
	OmsChargerErrorRecordService omsChargerErrorRecordService;
	@Resource
	OmsChargerDao omsChargerDao;
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("/getList")
	@ResponseBody
	public JsonForList getList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JsonForList isLogin = StringUtil.isLogin(request);
		if(isLogin.getCode().equals("400")){
			return isLogin;
		}
//		HttpSession session = request.getSession(); 
//		String login_oper_id = (String) session.getAttribute("login_oper_id"); 
		OmsChargerErrorRecord record = new OmsChargerErrorRecord();
		String charger_error_id = request.getParameter("charger_error_id");
		if(StringUtil.isNumeric(charger_error_id)){
			record.setCharger_error_id(Long.valueOf(charger_error_id));
		}
		String charger_id = request.getParameter("charger_id");
		if(StringUtil.isNotBlank(charger_id)){
			record.setCharger_id(charger_id);
		}
		String error_code = request.getParameter("error_code");
		if(StringUtil.isNotBlank(error_code)){
			record.setError_code(error_code);
		}
		String status = request.getParameter("status");
		if(StringUtil.isNumeric(status)){
			record.setStatus(Integer.valueOf(status));
		}
		String valid_flag = request.getParameter("valid_flag");
		if (StringUtils.isNumeric(valid_flag)) {
			record.setValid_flag(Integer.valueOf(valid_flag));
		}
		return omsChargerErrorRecordService.findList(record);
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("/getPageList")
	@ResponseBody
	public JsonForList getPageList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //搜索条件：可选
        String searchStr = request.getParameter("searchStr");
        if (StringUtil.isNotBlank(searchStr)) {
        	searchStr = new String(searchStr.getBytes("iso-8859-1"),"utf-8");
		}
        
		return omsChargerErrorRecordService.findPageList(ActionUtils.buildQueryParam(request, true),searchStr);
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("/update")
	@ResponseBody
	public JsonCommon update(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession(); 
		String login_oper_id = (String) session.getAttribute("login_oper_id"); 
		OmsChargerErrorRecord query = new OmsChargerErrorRecord();
		String charger_error_id = request.getParameter("charger_error_id");
		if(StringUtil.isNumeric(charger_error_id)){
			query.setCharger_error_id(Long.valueOf(charger_error_id));
		}else{
			return new JsonCommon(null, OmsConstant.ErrorCodeEnum.MessageMissParam.value(), OmsConstant.ErrorCodeEnum.MessageMissParam.desc());
		}
		OmsChargerErrorRecord record =  omsChargerErrorRecordService.findOneExample(query,null,null);
		if(record == null){
			return new JsonCommon(null, OmsConstant.ErrorCodeEnum.ParamTypeError.value(), OmsConstant.ErrorCodeEnum.ParamTypeError.desc());
		}
		String charger_id = request.getParameter("charger_id");
		if(StringUtil.isNotBlank(charger_id)){
			record.setCharger_id(charger_id);
		}
		String error_code = request.getParameter("error_code");
		if(StringUtil.isNotBlank(error_code)){
			record.setError_code(error_code);
		}
		String status = request.getParameter("status");
		if(StringUtil.isNumeric(status)){
			record.setStatus(Integer.valueOf(status));
		}
		String handle_text = request.getParameter("handle_text");
		if(StringUtil.isNotBlank(handle_text)){
			record.setHandle_text(handle_text);
		}
		String handle_time = request.getParameter("handle_time");//年月日时分秒
		if(StringUtil.isNotBlank(handle_time)){
			record.setHandle_time(StringUtil.StrToDate(handle_time));
		}
		String valid_flag = request.getParameter("valid_flag");
		if (StringUtils.isNumeric(valid_flag)) {
			record.setValid_flag(Integer.valueOf(valid_flag));
		}
		record.setUpdate_oper_id(login_oper_id);
		record.setUpdate_time(new Date());
		return omsChargerErrorRecordService.update(record);
	}
	
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("/getErrorRecordByChargerId")
	@ResponseBody
	public JsonForList getErrorRecordByChargerId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String charger_id = request.getParameter("charger_id");
		//开始需要查询电桩为故障状态的最新一条故障信息：现修正为该电桩的最近一条故障信息，不管该电桩是否故障
//		String sql = "";
//		Integer errorTotal = 0 ;
//		
//		String condition = " where charger_id = '" + charger_id + "'";
//		OmsCharger   omsCharger = omsChargerDao.findOneExampleByCondition(new OmsCharger(), condition);
//		switch(omsCharger.getCharger_type()){//设备电流类型:1--直流  2--交流 
//		case 1:
//			//先查询直流桩状态表
//			sql = "select count(*) as total from oms_charger_state_direct where  charge_status = 3 and valid_flag = 1 ";
//			List<Map<String, Object>> listDirect = omsChargerDao.findDataBySql(sql, null);
//			Map<String, Object> mapDirect = listDirect.get(0);
//			errorTotal = Integer.valueOf(mapDirect.get("total").toString());
//			
//		case 2:
//			//先查询直流桩状态表
//			sql = "select count(*) as total from oms_charger_state_alternate where  charge_status = 3 and valid_flag = 1 ";
//			List<Map<String, Object>> listAlternate = omsChargerDao.findDataBySql(sql, null);
//			Map<String, Object> mapAlternate = listAlternate.get(0);
//			errorTotal = Integer.valueOf(mapAlternate.get("total").toString());
//			
//		}
		
//		List<OmsChargerErrorRecord> list = new ArrayList<OmsChargerErrorRecord>();
//		if ( errorTotal>0 ) {
//			OmsChargerErrorRecord omsChargerErrorRecord =new OmsChargerErrorRecord();
//			omsChargerErrorRecord.setCharger_id(charger_id);
//			omsChargerErrorRecord = omsChargerErrorRecordService.findOneExample(omsChargerErrorRecord,"create_time","desc");
//			list.add(omsChargerErrorRecord);
//		}
		
//		JsonForList json = new JsonForList();
//		json.setList(list);
//		json.setCode(JsonForList.SuccessCode);

		
        String errormsg = null;
		
		if (StringUtil.isBlank(charger_id)) {
			errormsg = "ChargerID should not be all empty";
		} 
		
		JsonForList json = new JsonForList();
		if (errormsg != null)
		{
			json.setCode(JsonForList.FailCode);
			json.setMsg(errormsg);
		}else{
			List<OmsChargerErrorRecord> list = new ArrayList<OmsChargerErrorRecord>();
//			OmsChargerErrorRecord omsChargerErrorRecord =new OmsChargerErrorRecord();
//			omsChargerErrorRecord.setCharger_id(charger_id);
//			omsChargerErrorRecord = omsChargerErrorRecordService.findOneExample(omsChargerErrorRecord,"create_time","desc");
//			list.add(omsChargerErrorRecord);
			
			list = omsChargerErrorRecordService.findListByCondition(charger_id);
			
			json.setList(list);
			json.setCode(JsonForList.SuccessCode);
		}
		
		return json;
	}
	
}
